from typing import Any, Optional

class MockPin:
    """
    Mock GPIO Pin class
    Used for testing GPIO functionality without hardware
    """
    def __init__(self, pin_id: int, mode: str = 'output') -> None:
        self.pin_id = pin_id
        self.state = 0
        self.mode = mode
        self.pull_mode: Optional[str] = None
        self._interrupt_callback: Optional[callable] = None
        print(f"MockPin {self.pin_id} initialized in {self.mode} mode")

    def value(self, val: Any = None) -> int:
        if val is not None:
            old_state = self.state
            self.state = int(bool(val))
            print(f"MockPin {self.pin_id}: {old_state} → {self.state}")
            if self._interrupt_callback and old_state != self.state:
                try:
                    self._interrupt_callback(self)
                except Exception as e:
                    print(f"Interrupt callback error: {e}")
        return self.state

    def irq(self, handler: callable, trigger: int = None) -> None:
        self._interrupt_callback = handler
        print(f"MockPin {self.pin_id} interrupt handler set")

    def __repr__(self) -> str:
        return f"MockPin(pin_id={self.pin_id}, state={self.state}, mode='{self.mode}')"

def set_pin_high_mock(pin: MockPin) -> None:
    pin.value(1)

def set_pin_low_mock(pin: MockPin) -> None:
    pin.value(0)

def read_pin_state_mock(pin: MockPin) -> int:
    return pin.value()

def toggle_pin_mock(pin: MockPin) -> int:
    current_state = pin.value()
    new_state = 1 - current_state
    pin.value(new_state)
    return new_state
